const userModel = require('../model/userModel');

//根据ID查询用户
module.exports.findById = async function (id) {
    return await userModel.findById(id)
}
//查询所有用户
module.exports.find = async function (searchObj, page) {
    //分页查询
    let { current, size } = page;
    //获取数据总数
    let total = await userModel.countDocuments(searchObj);
    //根据总数/每页显示数得到总页数
    let tatalPage = Math.ceil(total / size);
    //limit是每页显示数，skip通过（当前页-1）*每页显示数
    let rows = await userModel
        .find(searchObj)
        .limit(parseInt(size))
        .skip((current - 1) * size);
    return {
        current, size, total, tatalPage, rows
    }
}
 
//增加用户
module.exports.insert = async function (user) {
    return await userModel.create(user);
}